﻿
Imports idsysctx
Imports occommon
Imports occommon.response

Public MustInherit Class oper
    Inherits requestProcessor

    Protected MustOverride Overloads Function process(ByVal site As String, _
                                                      ByVal user As String, _
                                                      ByVal context As httpContext) As Boolean
    Protected MustOverride Function useUserToLogin() As Boolean

    Protected Shared Function setSuccessStatus(ByVal context As httpContext) As Boolean
        Return context.setstatuscode(statusCode.success, description.success)
    End Function

    Public Overrides Function process(ByVal context As httpContext) As Boolean
        'user & site operations always need a user input
        Dim user As String = Nothing
        context.userInput(query.user, cookie.user, user)
        If isemptystring(user) Then
            Return context.setstatuscode(statusCode.noUserInput, description.noUserInput)
        End If

        Dim site As String = Nothing
        context.userInput(query.site, cookie.site, site)
        If isSharedCookieSite(site) Then
            Return context.setstatuscode(statusCode.invalidSiteInput, description.invalidSiteInput)
        End If

        Dim usrk As String = Nothing
        context.userInput(query.userSiteRandomKey, cookie.userSiteRandomKey, usrk)
        If isemptystring(usrk) Then
            Return context.setstatuscode(statusCode.noOnlineCookieUserSiteRandomKeyInput, _
                                         description.noOnlineCookieUserSiteRandomKeyInput)
        End If

        Dim idctx As idcontext = Nothing
        If useUserToLogin() Then
            idctx = createIdContext(user, usrk, context, True)
        Else
            If isemptystring(site) Then
                Return context.setstatuscode(statusCode.noSiteInput, description.noSiteInput)
            End If
            idctx = createIdContext(site, usrk, context, True)
        End If

        assert(Not idctx Is Nothing)
        If idctx.loggedIn() Then
            If useUserToLogin() Then
                context.setforevercookie(cookie.user, user)
            Else
                context.setforevercookie(cookie.site, site)
            End If
            context.setforevercookie(cookie.userSiteRandomKey, usrk)
            If process(site, user, context) Then
                Return True
            Else
                Return context.setstatuscode(statusCode.internalError, description.internalError)
            End If
        Else
            Return context.setstatuscode(statusCode.onlineCookieUserSiteRandomKeyError, _
                                         description.onlineCookieUserSiteRandomKeyError)
        End If
    End Function
End Class
